package cn.maidouya.dictionary.controller;
import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.dictionary.domain.DictionaryDetail;
import cn.maidouya.dictionary.query.DictionaryDetailQuery;
import cn.maidouya.dictionary.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
@Api(tags = "字典明细接口",description = "字典明细接口")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @PutMapping
    @ApiOperation(value = "添加和修改字典明细",notes = "添加和修改字典明细")
    public AjaxResult addAndUpdata(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.add(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("操作失败:" +e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除字典明细",notes = "删除字典明细")
    public AjaxResult del(@PathVariable Long id){
        try {
            dictionaryDetailService.del(id);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("操作失败:" +e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查找一个字典明细",notes = "查找一个字典明细")
    public DictionaryDetail findOne(@PathVariable("id") Long id){
   return dictionaryDetailService.finOne(id);
    }
    @GetMapping
    @ApiOperation(value = "查找全部典明细",notes = "查找全部典明细")
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.finAll();
    }
    @PostMapping
    @ApiOperation(value = "查找分页典明细",notes = "查找分页典明细")
    public PageList<DictionaryDetail> findPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.findPage(dictionaryDetailQuery);
    }
}